<template>
  <NuxtLayout name="admin" menuActive="/admin/personnel">
    <div class="card">
      <el-page-header @back="useRouter().go(-1)">
        <template #extra>
          <el-button @click="addFn">添加管理员</el-button>
        </template>
      </el-page-header>
      <div style="height: 18px;"></div>
      <el-table :data="tableData" border show-overflow-tooltip>
        <el-table-column prop="name" label="管理员姓名" />
        <el-table-column prop="nickName" label="管理员昵称" />
        <el-table-column prop="phoneNo" label="管理员手机号码" />
        <el-table-column prop="institutionRoleName" label="管理员角色名称" />
        <el-table-column label="操作" fixed="right" width="75">
          <template #default="scope">
            <el-button type="danger" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="card_pagin">
        <el-pagination background layout="prev, pager, next, total" :total="total" :page-size="listForm.pageSize"
          @change="pageChangeFn" />
      </div>
    </div>
    <MyForm :visible="formVisible" :forms="forms" :title="formTitle" @pick="formPickFn" />
  </NuxtLayout>
</template>

<script setup>
import { adminPageList, addInstitutionAdmin, deleteInstitutionAdmin } from '~/server/api'
import MyForm from './form'
// TDK
const prevHost = useRequestHeaders()["x-forwarded-proto"];
const host = useRequestHeaders()["host"];
const fullPath = useRoute()["fullPath"];
useSeoMeta({
  title: "技考邦_管理人员",
  keywords: "技考邦,管理人员",
  description: "技考邦，管理人员。",
  ogTitle: "技考邦_管理人员",
  ogDescription: "技考邦，管理人员。",
  ogType: "article",
  ogUrl: `${prevHost}://${host}${fullPath}`,
});
// 列表
const listForm = ref({
  pageNum: 1,
  pageSize: 20
})
const tableData = ref([])
const total = ref(0)
const getList = async () => {
  let { data: listRes } = await useAsyncData('adminPageList', () => useServerRequest(adminPageList, {
    method: 'post',
    body: JSON.stringify(listForm.value)
  }))
  if (listRes.value && listRes.value.errno === 0) {
    tableData.value = listRes.value.result.list;
    total.value = listRes.value.result.total;
  }
}
getList()
// 分页
const pageChangeFn = (val) => {
  listForm.value.pageNum = val;
  getList()
}
// 添加
const forms = ref({})
const formVisible = ref(false)
const formTitle = ref('')
const addFn = () => {
  forms.value.phoneNo = '';
  forms.value.institutionRoleId = null;
  formTitle.value = '添加管理员';
  formVisible.value = true;
}
// 删除
const delFn = (val) => {
  ElMessageBox.confirm('是否删除当前数据', {
    showClose: false,
    closeOnClickModal: false,
    closeOnPressEscape: false
  }).then(async () => {
    let delRes = await useServerRequest(deleteInstitutionAdmin, {
      method: 'post',
      body: JSON.stringify({
        phoneNo: val.phoneNo
      })
    })
    if (delRes.errno === 0) {
      ElMessage({
        message: '删除成功',
        type: 'success'
      })
      getList()
      return false;
    }
  }).catch(() => {})
}
// 取消 确认
const formPickFn = async (e) => {
  if (e.form) {
    if (!/^1[3456789]\d{9}$/.test(e.form.phoneNo)) {
      ElMessage({
        message: '管理员手机号有误',
        type: 'warning',
        appendTo: document.querySelector('.el-dialog') || document.body
      })
      return false
    }
    const loading = ElLoading.service({ text: '请求中...', target: document.querySelector('.el-dialog') })
    let classRes = await useServerRequest(addInstitutionAdmin, {
      method: 'post',
      body: JSON.stringify(e.form)
    })
    if (classRes.errno === 0) {
      ElMessage({
        message: '添加成功',
        type: 'success'
      })
      getList()
      formVisible.value = e.visible;
    }
    loading.close()
    return false;
  }
  formVisible.value = e.visible;
}
</script>

<style lang="less" scoped>
.card {
  background: #FFFFFF;
  border-radius: 10px;
  border: 1px solid #EEEEEE;
  padding: 35px 25px;

  &_pagin {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>